from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from apps.meiduo_admin.serializers.spec_serializers import OptionModelSerializer, OptSpecSimpleSerializer
from apps.meiduo_admin.serializers.spu_serializers import  *
from apps.goods.models import SPU
from apps.meiduo_admin.paginations import *

class SPUView(ModelViewSet):
    queryset = SPU.objects.all().order_by("pk")
    serializer_class = SPUModelSerializer
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")

        if keyword:
            return self.queryset.filter(name__contains=keyword)

        return self.queryset.all()


# 序列化返回品牌信息
class BrandSimpleView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSimpleSerializer

# 商品可选分类表：
class SPUCateSimpleView(ListAPIView):
    queryset = GoodsCategory.objects.all()
    serializer_class = SPUCateSimpleSerializer

    def get_queryset(self):
        parent_id = self.kwargs.get("pk")

        # 此时表示是二级或者三级分类
        if parent_id:
            return self.queryset.filter(parent_id = parent_id)

        # 此时表示一级分类
        else:
            return self.queryset.filter(parent_id = None)

# 规格表
class OptionView(ModelViewSet):
    queryset = SpecificationOption.objects.all()
    serializer_class = OptionModelSerializer
    pagination_class = MyPage

# 规格选项表
class OptSpecSimpleView(ListAPIView):
    queryset = SPUSpecification.objects.all()
    serializer_class = OptSpecSimpleSerializer